3 * Implements Special:Unblock
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * A special page for unblocking users
27 * @ingroup SpecialPage
29 class SpecialUnblock
extends SpecialPage
{
35 public function __construct() {
36 parent
::__construct( 'Unblock', 'block' );
39 public function doesWrites() {
43 public function execute( $par ) {
44 $this->checkPermissions();
45 $this->checkReadOnly();
47 list( $this->target
, $this->type
) = SpecialBlock
::getTargetAndType( $par, $this->getRequest() );
48 $this->block
= Block
::newFromTarget( $this->target
);
49 if ( $this->target
instanceof User
) {
50 # Set the 'relevant user' in the skin, so it displays links like Contributions,
51 # User logs, UserRights, etc.
52 $this->getSkin()->setRelevantUser( $this->target
);
56 $this->outputHeader();
58 $out = $this->getOutput();
59 $out->setPageTitle( $this->msg( 'unblockip' ) );
61 $form = HTMLForm
::factory( 'ooui', $this->getFields(), $this->getContext() );
62 $form->setWrapperLegendMsg( 'unblockip' );
63 $form->setSubmitCallback( [ __CLASS__
, 'processUIUnblock' ] );
64 $form->setSubmitTextMsg( 'ipusubmit' );
65 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
67 if ( $form->show() ) {
68 switch ( $this->type
) {
70 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target
) );
72 case Block
::TYPE_USER
:
73 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target
) );
75 case Block
::TYPE_RANGE
:
76 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target
) );
79 case Block
::TYPE_AUTO
:
80 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target
) );
86 protected function getFields() {
90 'label-message' => 'ipaddressorusername',
97 'label-message' => 'ipaddressorusername',
101 'label-message' => 'ipbreason',
105 if ( $this->block
instanceof Block
) {
106 list( $target, $type ) = $this->block
->getTargetAndType();
108 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
109 # User:Foo, and we've just got any block, auto or not, that applies to a target
110 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
111 # so don't show any distinction between unblocked IPs and autoblocked IPs
112 if ( $type == Block
::TYPE_AUTO
&& $this->type
== Block
::TYPE_IP
) {
113 $fields['Target']['default'] = $this->target
;
114 unset( $fields['Name'] );
116 $fields['Target']['default'] = $target;
117 $fields['Target']['type'] = 'hidden';
120 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
121 SpecialPage
::getTitleFor( 'Contributions', $target->getName() ),
124 $fields['Name']['raw'] = true;
126 case Block
::TYPE_USER
:
127 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
128 $target->getUserPage(),
131 $fields['Name']['raw'] = true;
134 case Block
::TYPE_RANGE
:
135 $fields['Name']['default'] = $target;
138 case Block
::TYPE_AUTO
:
139 $fields['Name']['default'] = $this->block
->getRedactedName();
140 $fields['Name']['raw'] = true;
141 # Don't expose the real target of the autoblock
142 $fields['Target']['default'] = "#{$this->target}";
145 // target is hidden, so the reason is the first element
146 $fields['Target']['autofocus'] = false;
147 $fields['Reason']['autofocus'] = true;
150 $fields['Target']['default'] = $this->target
;
151 unset( $fields['Name'] );
158 * Submit callback for an HTMLForm object
160 * @param HTMLForm $form
161 * @return array|bool Array(message key, parameters)
163 public static function processUIUnblock( array $data, HTMLForm
$form ) {
164 return self
::processUnblock( $data, $form->getContext() );
170 * Change tags can be provided via $data['Tags'], but the calling function
171 * must check if the tags can be added by the user prior to this function.
174 * @param IContextSource $context
175 * @throws ErrorPageError
176 * @return array|bool Array( Array( message key, parameters ) ) on failure, True on success
178 public static function processUnblock( array $data, IContextSource
$context ) {
179 $performer = $context->getUser();
180 $target = $data['Target'];
181 $block = Block
::newFromTarget( $data['Target'] );
183 if ( !$block instanceof Block
) {
184 return [ [ 'ipb_cant_unblock', $target ] ];
187 # T17810: blocked admins should have limited access here. This
188 # won't allow sysops to remove autoblocks on themselves, but they
189 # should have ipblock-exempt anyway
190 $status = SpecialBlock
::checkUnblockSelf( $target, $performer );
191 if ( $status !== true ) {
192 throw new ErrorPageError( 'badaccess', $status );
195 # If the specified IP is a single address, and the block is a range block, don't
196 # unblock the whole range.
197 list( $target, $type ) = SpecialBlock
::getTargetAndType( $target );
198 if ( $block->getType() == Block
::TYPE_RANGE
&& $type == Block
::TYPE_IP
) {
199 $range = $block->getTarget();
201 return [ [ 'ipb_blocked_as_range', $target, $range ] ];
204 # If the name was hidden and the blocking user cannot hide
205 # names, then don't allow any block removals...
206 if ( !$performer->isAllowed( 'hideuser' ) && $block->mHideName
) {
207 return [ 'unblock-hideuser' ];
210 $reason = [ 'hookaborted' ];
211 if ( !Hooks
::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
216 if ( !$block->delete() ) {
217 return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
220 Hooks
::run( 'UnblockUserComplete', [ $block, $performer ] );
222 # Unset _deleted fields as needed
223 if ( $block->mHideName
) {
224 # Something is deeply FUBAR if this is not a User object, but who knows?
225 $id = $block->getTarget() instanceof User
226 ?
$block->getTarget()->getId()
227 : User
::idFromName( $block->getTarget() );
229 RevisionDeleteUser
::unsuppressUserName( $block->getTarget(), $id );
232 # Redact the name (IP address) for autoblocks
233 if ( $block->getType() == Block
::TYPE_AUTO
) {
234 $page = Title
::makeTitle( NS_USER
, '#' . $block->getId() );
236 $page = $block->getTarget() instanceof User
237 ?
$block->getTarget()->getUserPage()
238 : Title
::makeTitle( NS_USER
, $block->getTarget() );
242 $logEntry = new ManualLogEntry( 'block', 'unblock' );
243 $logEntry->setTarget( $page );
244 $logEntry->setComment( $data['Reason'] );
245 $logEntry->setPerformer( $performer );
246 if ( isset( $data['Tags'] ) ) {
247 $logEntry->setTags( $data['Tags'] );
249 $logId = $logEntry->insert();
250 $logEntry->publish( $logId );
256 * Return an array of subpages beginning with $search that this special page will accept.
258 * @param string $search Prefix to search for
259 * @param int $limit Maximum number of results to return (usually 10)
260 * @param int $offset Number of results to skip (usually 0)
261 * @return string[] Matching subpages
263 public function prefixSearchSubpages( $search, $limit, $offset ) {
264 $user = User
::newFromName( $search );
266 // No prefix suggestion for invalid user
269 // Autocomplete subpage as user list - public to allow caching
270 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
273 protected function getGroupName() {